var utils = require('../../../utils/util.js');
var canSend;
var intervalID;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipval: '获取验证码',
    phonenum: '',
    verifycode: ''
  },
  timeerid:0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    canSend = true;
    intervalID = -1;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('hide:' + intervalID);
    if (intervalID) {
      clearTimeout(intervalID);
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('hide:' + intervalID);
    if (intervalID) {
      clearTimeout(intervalID);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sendMessageAction: function () {
    if (utils.checkPhoneNumber(this.data.phonenum) == false) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }
    if (canSend == false) {
      return;
    }
    
    canSend = false;
    var url = app.globalData.webUrl + '/Sms/smssend';
    var params = {
      mobile:this.data.phonenum
    };
    app.loadDataFromServer(url,this.messageCallBack,params)
  },
  messageCallBack:function(res){
    console.log(res);
    if (res.status == 'ok'){
      this.setData({
        smsid: res.result
      });
      this.startTime();
      
    }
    else{
      wx.showToast({
        title: res.msg,
        icon: 'none'
      });
      canSend = true;
    }
  },
  // -------------------------------------------------------------------------

  bindPhoneAction: function () {
    if (this.data.phonenum == '' || this.data.verifycode == ''){
      wx.showToast({
        title: '请输入有效的验证信息',
        icon: 'none'
      });
      return;
    }
    var url = app.globalData.webUrl + '/Sms/checkBindPhone';
    var params = {
      smsid: this.data.smsid,
      code: this.data.verifycode
    };
    app.loadDataFromServer(url,this.bindPhoneCallBack,params);
  },
  bindPhoneCallBack:function(res){
    console.log(res);
    if (res.status == 'ok'){
      app.globalData.userinfo.phone = this.data.phonenum;
      wx.showToast({
        title: res.msg,
        duration:2000
      });

      this.timeerid = setTimeout(function () {
        wx.navigateBack({});
        clearTimeout(this.timeerid);
      }, 2000);
      
    }

    
  },
  //------------------------------------
  startTime: function () {
    var mTotal = 60;
    this.setData({
      tipval: '剩余' + mTotal + 's'
    });
    this.startTimeAction(mTotal);
  },
  startTimeAction: function (timeval) {
    var that = this;
    intervalID = setTimeout(function () {
      clearTimeout(intervalID);
      timeval--;
      console.log(timeval);
      if (timeval < 0) {
        canSend = true;
        that.setData({
          tipval: '发送验证码'
        });
      }
      else {
        that.setData({
          tipval: '剩余' + timeval + 's'
        });
        that.startTimeAction(timeval);
      }
    }, 1000);
  },
  //------------------INPUT------------------------------
  phoneInput:function(e){
    var val = e.detail.value;
    this.setData({
      phonenum: val
    });
  },
  codeInput:function(e){
    var val = e.detail.value;
    this.setData({
      verifycode:val
    });
  }
})